package com.kfm.ssm.service.impl;

import com.kfm.ssm.domain.Account;
import com.kfm.ssm.mapper.AccountMapper;
import com.kfm.ssm.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper mapper;

    @Override
    public List findAll() {
        System.out.println("findAll");

        return mapper.selectAll();
    }

    @Override
    @Transactional
    public void transfer(Integer sourceId, Integer targetId, Double money) {
        System.out.println("transfer");
        Account account = mapper.selectById(sourceId);
        Account account1 = mapper.selectById(targetId);

        account.setMoney(account.getMoney() - money);
        account1.setMoney(account1.getMoney() + money);

        mapper.updateMoney(account);
        int i = 3 / 0;
        mapper.updateMoney(account1);
    }
}
